﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Internal.Runtime.Models;
using Runtime.Services.Battle;
using Runtime.Services.BattleAttack;

namespace Runtime.Models {

    /// <summary>
    /// 进攻方数据集
    /// </summary>
    public interface IDFR_BattleAttackSourceModelTD : IDFR_BattleAttackSourceModelBase {

        new IDFR_BattleAttackSourceModelTDSetter AsSetter();

        new IReadOnlyList<IDFR_BattleSkillModelTD> SkillList { get; }

        /// <summary>
        /// 所属单元格索引
        /// </summary>
        DFR_BattleCellIndex CellIndex { get; }
    }

    public interface IDFR_BattleAttackSourceModelTDSetter : IDFR_BattleAttackSourceModelBaseSetter, IDFR_BattleAttackSourceModelTD {
    }

    public abstract class DFR_BattleAttackSourceModelTD : DFR_BattleAttackSourceModelBase,
        IDFR_BattleAttackSourceModelTD, IDFR_BattleAttackSourceModelTDSetter, IDFR_BattleAttackSourceModelTDInternal {

        public new IDFR_BattleAttackSourceModelTDSetter AsSetter() => this;

        protected override IEnumerable<IDFR_BattleAttackTarget> Targets =>
            DFR_BattleTD.DataCenter.Models.AttackTargetDictionary.Where(kv => kv.Value.AsAttackTarget().IsEnabled).OrderBy(kv => kv.Key.Index).Select(kv => kv.Value.AsAttackTarget());

        private DFR_BattleCellIndex m_CellIndex;
        public DFR_BattleCellIndex CellIndex {
            [DebuggerStepThrough]
            [DebuggerNonUserCode]
            get => m_CellIndex;
            [DebuggerStepThrough]
            [DebuggerNonUserCode]
            set {
                m_CellIndex = value;
                m_isCellIndexDirty = true;
            }
        }

        protected override DFR_BattleMapPosition MapPosition => Axis;

        private bool m_isCellIndexDirty = true;
        private DFR_BattleCellAxis m_Axis;
        public DFR_BattleCellAxis Axis {
            [DebuggerStepThrough]
            [DebuggerNonUserCode]
            get {
                if (m_isCellIndexDirty) {
                    m_isCellIndexDirty = false;
                    m_Axis = CellIndex.ToAxis();
                }
                return m_Axis;
            }
        }

        IReadOnlyList<IDFR_BattleSkillModelTD> IDFR_BattleAttackSourceModelTD.SkillList => (IReadOnlyList<IDFR_BattleSkillModelTD>)SkillList;

        protected override void Dispose() {
            CellIndex = DFR_BattleCellIndex.Invalid;
            base.Dispose();
        }

        void IDFR_BattleAttackSourceModelTDInternal.MoveTo(DFR_BattleCellIndex cellIndex) {
            CellIndex = cellIndex;
        }

        void IDFR_BattleAttackSourceModelTDInternal.Initialize(DFR_BattleCellIndex cellIndex) {
            CellIndex = cellIndex;
        }
    }
}

namespace Internal.Runtime.Models {

    public interface IDFR_BattleAttackSourceModelTDInternal : IDFR_BattleAttackSourceModelBaseInternal {

        void Initialize(DFR_BattleCellIndex cellIndex);

        void MoveTo(DFR_BattleCellIndex cellIndex);
    }
}